package com.gitee.jnexus.nicy.verticles;

import io.vertx.config.ConfigRetriever;
import io.vertx.config.ConfigRetrieverOptions;
import io.vertx.config.ConfigStoreOptions;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.jdbc.JDBCClient;
import io.vertx.ext.sql.ResultSet;
import io.vertx.ext.sql.SQLConnection;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class NicyBootVerticle extends AbstractVerticle {

    @Override
    public void start(Future<Void> startFuture) throws Exception {
        log.info("Starting NicyBootVerticle...");


        // http://vertx.io/docs/vertx-config/java/
//        ConfigStoreOptions httpStore = new ConfigStoreOptions()
//            .setType("http")
//            .setConfig(new JsonObject()
//                .put("host", "localhost").put("port", 8080).put("path", "/conf"));

        ConfigStoreOptions fileStore = new ConfigStoreOptions()
            .setType("file")
            .setConfig(new JsonObject()
                .put("path", "config-jdbc.json"));

        ConfigStoreOptions sysPropsStore = new ConfigStoreOptions().setType("sys");


        ConfigRetrieverOptions options = new ConfigRetrieverOptions()
            .addStore(fileStore).addStore(sysPropsStore);

        ConfigRetriever retriever = ConfigRetriever.create(vertx, options);

        retriever.getConfig(ar -> {
            try {
                if (ar.failed()) {
                    log.error("Failed to retrieve the configuration", ar.cause());
                } else {
                    JsonObject config = ar.result();


                    JDBCClient client = JDBCClient.createShared(vertx, config, "HikariCPDataSource");
                    client.getConnection(res -> {
                        if (res.succeeded()) {
                            SQLConnection connection = res.result();
                            connection.query("SELECT customer.fname, customer.lname, item.description\n" +
                                "FROM customer\n" +
                                "    JOIN orderinfo ON customer.customer_id = orderinfo.customer_id\n" +
                                "    JOIN orderline ON orderinfo.orderinfo_id = orderline.orderinfo_id\n" +
                                "    JOIN item ON orderline.item_id = item.item_id\n" +
                                "WHERE\n" +
                                "    customer.fname = 'Ann' AND\n" +
                                "    customer.lname = 'Stones';", res2 -> {
                                if (res2.succeeded()) {
                                    ResultSet rs = res2.result();
                                    // 用结果集results进行其他操作
                                    rs.getRows().forEach(row -> {
                                        log.info(row.toString());
                                    });
                                }
                            });
                        } else {
                            // 获取连接失败 - 处理失败的情况
                        }
                    });


                    super.start(startFuture);
                }
            } catch (Exception e) {
                log.error("Exception on starting verticle", e);
            }
        });
    }
}
